package org.tamal.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Map;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.ChapterAutoNumber;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * This class shall generate PDF report.
 * @author Tamal Kanti Nath
 */
public final class PDFReport {

    private static final int DEFAULT_SIZE = 12;
    private static final Font H1 = new Font(FontFamily.HELVETICA, 16,
            Font.BOLD, BaseColor.BLACK);
    private static final Font H2 = new Font(FontFamily.HELVETICA, 14,
            Font.BOLDITALIC, BaseColor.BLACK);
    private static final Font H3 = new Font(FontFamily.HELVETICA, DEFAULT_SIZE,
            Font.BOLD, BaseColor.BLACK);
    private File file;
    private Document document = new Document();
    private PdfWriter writer;

    /**
     * Creates a PDF report.
     * @param fileName the file name
     * @throws FileNotFoundException if the file cannot be created
     */
    public PDFReport(String fileName) throws FileNotFoundException {
        file = new File(fileName);
        try {
            writer = PdfWriter.getInstance(document,
                    new FileOutputStream(file));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        document.open();
        document.addAuthor("Tamal Kanti Nath");
        document.addCreator("tamal");
        document.addHeader("Application", "Console Application");
        document.addKeywords("PDF");
        document.addSubject("Console Application PDF report");
        document.addTitle("PDF Report");
    }

    /**
     * Adds environment variables.
     */
    public void addSystemInfo() {
        Chapter ch = new ChapterAutoNumber(new Paragraph("Information", H1));
        Section sec = ch.addSection(new Paragraph("System Properties", H2));
        sec.add(getTable(System.getProperties()));
        sec = ch.addSection(new Paragraph("Environment Variables", H2));
        sec.add(getTable(System.getenv()));
        try {
            document.add(ch);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }

    private static <K, V> PdfPTable getTable(Map<K, V> map) {
        int k = 0;
        int v = 0;
        for (Map.Entry<K, V> entry : map.entrySet()) {
            k += entry.getKey().toString().length();
            v += entry.getValue().toString().length();
        }
        PdfPTable table = new PdfPTable(new float[] {k, v});
        table.setSpacingBefore(DEFAULT_SIZE);
        table.setSpacingAfter(DEFAULT_SIZE);
        table.addCell(new Phrase("Name", H3));
        table.addCell(new Phrase("Value", H3));
        for (Map.Entry<K, V> entry : map.entrySet()) {
            table.addCell(entry.getKey().toString());
            table.addCell(entry.getValue().toString());
        }
        return table;
    }
    /**
     * Close the PDF report.
     */
    public void close() {
        document.close();
        writer.close();
    }
}
